﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using System.IO;

public partial class controls_admin_VoteEdit : Pages_Base
{
    string path;
    protected void Page_Load(object sender, EventArgs e)
    {
        path = Server.MapPath("~/upload/xml/vote.xml");
        if (!IsPostBack)
            loadVote();
    }
    private void loadVote()
    {
        try
        {
            if (!File.Exists(path))
                return;
            DataSet objDS = new DataSet();
            objDS.ReadXml(path);
            DataTable objResult = objDS.Tables[1];
            DataTable objQuestion = objDS.Tables[0];
            txtQuestion.Text = objQuestion.Rows[0]["QUESTION"].ToString();
            string val = string.Empty;
            foreach (DataRow row in objResult.Rows)
                val += row["ANSWER"].ToString() + ";";
            txtAnswer.Text = val;
        }
        catch { }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (!File.Exists(path))
                return;
            DataSet objDS = new DataSet();
            objDS.ReadXml(path);
            DataTable objResult = objDS.Tables[0];
            DataRowCollection lrow = objResult.Rows;
            DataTable objQuestion = objDS.Tables[1];
            objQuestion.Rows[0]["QUESTION"] = txtQuestion.Text.Trim();
            string newAnswer = txtAnswer.Text.Trim();
            string[] perNewAns = newAnswer.Split(new Char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < perNewAns.Length; i++)
            {
                if (i < lrow.Count)
                    lrow[i]["ANSWER"] = perNewAns[i];
                else
                {
                    DataRow nrow = objResult.NewRow();
                    nrow["ANSWER"] = perNewAns[i];
                    nrow["NUMBER"] = "0";
                    nrow["RATE"] = "0";
                    lrow.Add(nrow);
                }
            }
            if (base.writeVote(objDS, false))
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thay đổi thành công !");
            else
                divMessage.InnerHtml = O2S_Message.Warning("Lỗi", "Chưa thực hiện được vui lòng thử lại sau !");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: " + ex.Message);
        }
    }
}
